<?php

/**
 * AVE.cms
 *
 * @package AVE.cms
 * @filesource
 */

/**
 * Постраничная навигация документа
 *
 * @param string $text	текст многострочной части документа
 * @return string
 */
function document_pagination($text)
{
	global $AVE_Core;

// IE8                    <div style="page-break-after: always"><span style="display: none">&nbsp;</span></div>
// Chrome                 <div style="page-break-after: always; "><span style="DISPLAY:none">&nbsp;</span></div>
// FF                     <div style="page-break-after: always;"><span style="display: none;">&nbsp;</span></div>
	$pages = preg_split('#<div style="page-break-after:[; ]*always[; ]*"><span style="display:[ ]*none[;]*">&nbsp;</span></div>#i', $text);
	$total_page = @sizeof($pages);

	if ($total_page > 1)
	{
		$text = @$pages[get_current_page('artpage')-1];

		$page_nav = ' <a class="pnav" href="index.php?id=' . $AVE_Core->curentdoc->Id
			. '&amp;doc=' . (empty($AVE_Core->curentdoc->document_alias) ? prepare_url($AVE_Core->curentdoc->document_title) : $AVE_Core->curentdoc->document_alias)
			. '&amp;artpage={s}'
//			. ((isset($_REQUEST['apage']) && is_numeric($_REQUEST['apage'])) ? '&amp;apage=' . $_REQUEST['apage'] : '')
//			. ((isset($_REQUEST['page']) && is_numeric($_REQUEST['page'])) ? '&amp;page=' . $_REQUEST['page'] : '')
			. '">{t}</a> ';
		$page_nav = get_pagination($total_page, 'artpage', $page_nav, get_settings('navi_box'));

		$text .= rewrite_link(''.$page_nav.'');
	}

	return $text;
}
function changeImage($Inhalt) 
{
	$_size = '%1$s%2$ux%3$u';
	preg_match_all("/\< *[img][^\>]*[.]*\>/i", $Inhalt, $matches);
	if(isset($matches)){
		foreach($matches[0] as $match)
		{
			preg_match_all("/(src|height|width|align|noresize|alt|title)*= *[\"\']{0,1}([^\"\'\>]*)/i",$match, $m);
			$images[] = array_combine($m[1],$m[2]);
		}
	}	
	if(isset($images))
	{
		$img_data = @getimagesize($base_dir . $filename);
		
 		foreach($images as $key=>$match)
		{
			//$size = (@$match['height'] >= @$match['width']) ? @$match['height'] : @$match['width'];
			
			$height = (@$match['height'] < @$match['width']) ? '120' : '150';
			$width = (@$match['width'] < @$match['height']) ? '120' : '150';
			
			
			//$size = '400';
			@$margin = ($match['align'] == "left") ? 'right' : 'left';
			//$margin = ($match['align'] == "left") ? 'right' : 'left';
			@$align = ($match['align'] != '') ? "align=\"".$match['align']."\" style=\"margin-".$margin.":5px; float:".$match['align']."\"" : '';
			
	
$namepos = strrpos($match['src'], "/");
if ($namepos !== false) {
	$filename = substr($match['src'], ++$namepos);
	$base_dir = BASE_DIR . str_replace($filename,'',$match['src']);
}	
	$img_data2 = @getimagesize($base_dir . $filename);

	$width2 = (($img_data2[0] >= $img_data2[1]) ? '600' : '0');
	$height2 = (($img_data2[1] >= $img_data2[0]) ? '600' : '0');


			
			$imgsrc = make_thumbnail(array("link" => $match['src'], "size" => sprintf($_size, "c", $width, $height)));
			$hrefimgsrc = make_thumbnail(array("link" => $match['src'], "size" => sprintf($_size, "r", $width2, $height2)));
			
//$thumb_id = "thumb_" . $size . "_" . $filename;	

if(file_exists(BASE_DIR . $imgsrc)) {
	$img_data = @getimagesize(BASE_DIR . $imgsrc);
}			
					
			
			
//			@$patern[] = "img href=\"/thumb.php?file=".$match['src']."&amp;x_width=700\" class=\"thumb pic\"  border=\"0\" ".$align." ".$img_data['3']." rell=\"prettyPhoto[gallery]\" rel=\"".$match['src']."\" title=\"".$match['title']."\" name=\"".$match['alt']."\"  alt=\"\" src='/thumb.php?file=".$match['src']."&amp;x_width=".$size."'";
			
			$patern[] = "img href='".$hrefimgsrc."' class='thumb pic' border='0' ".$align." ".$img_data['3']." rell='prettyPhoto[gallery]' rel='".$hrefimgsrc."' title='".$match["alt"]."' alt='".$match["alt"]."' src='".$imgsrc."'";

//print_r($patern);
//exit;


		}
		if(@$images[0]['noresize'] != '1')$Inhalt = preg_replace($matches[0], $patern, $Inhalt);
	}	
	//"<span class='preload_img preloading_background'>".."</span>"
	return $Inhalt;
} 







//var	$_size = '%1$s%2$ux%3$u';
//make_thumbnail(array("link" => $doc["preimage"], "size" => sprintf(self::_size, "c", "$1", "$2")))








/**
 * Формирование поля документа в соответствии с шаблоном отображения
 *
 * @param int $field_id	идентификатор поля
 * @return string
 */
function document_get_field($field_id)
{
	global $AVE_Core;

	if (is_array($field_id)) $field_id = $field_id[1];

	$document_fields = get_document_fields($AVE_Core->curentdoc->Id);
	if (empty($document_fields[$field_id])) return '';

	$pole = "field_value_".$_SESSION['user_language'];

	$field_value = trim($document_fields[$field_id][$pole]);

	$tpl_field_empty = $document_fields[$field_id]['tpl_field_empty'];

	if ($field_value == '' && $tpl_field_empty) return '';

	$field_type = $document_fields[$field_id]['rubric_field_type'];

	$rubric_field_template = trim($document_fields[$field_id]['rubric_field_template']);

//	$field_value = parse_hide($field_value);
//	$field_value = ($length != '') ? truncate_text($field_value, $length, '…', true) : $field_value;

	$func='get_field_'.$field_type;
	if(!is_callable($func)) $func='get_field_default';
	$field_value=$func($field_value,'doc',$field_id,$rubric_field_template,$tpl_field_empty);
	
	if (isset($_SESSION['user_adminmode']) && $_SESSION['user_adminmode'] == 1)
	{
		$wysmode = false;

		if (defined('UGROUP') && UGROUP == 1) $wysmode = true;
		elseif (isset($_SESSION[RUB_ID . '_alles'])   && $_SESSION[RUB_ID . '_alles']   == 1) $wysmode = true;
		elseif (isset($_SESSION[RUB_ID . '_editall']) && $_SESSION[RUB_ID . '_editall'] == 1) $wysmode = true;
		elseif (isset($_SESSION[RUB_ID . '_editown']) && $_SESSION[RUB_ID . '_editown'] == 1 &&
				isset($_SESSION['user_id']) && $_SESSION['user_id'] == $document_fields[$field_id]['document_author_id']) $wysmode = true;

		if ($wysmode)
		{
			$f_value .= '<link rel="stylesheet" href="inc/stdimage/gear.css" type="text/css" />';
			$f_value .= '<div class="contextual-links-wrapper contextual-links-processed">';
			$f_value .= '<a class="contextual-links-trigger" href="javascript:void(0);" onclick=window.open("'.ABS_PATH.'admin/index.php?do=docs&action=edit&closeafter=1&RubrikId=' . RUB_ID . '&Id=' . ((int)$_REQUEST['id'])
				. '&pop=1&feld=' . $field_id . '#' . $field_id . '","EDIT","left=0,top=0,width=1300,height=900,scrollbars=1");></a>';
			$f_value_end .= '</div>';
		}
	}
	return @$f_value.@$field_value.@$f_value_end;
}

/**
 * Функция получения содержимого поля для обработки в шаблоне рубрики
 *
 * @param int $field_id	идентификатор поля, для [tag:fld:12] $field_id = 12
 * @param int $length	необязательный параметр,
 * 						количество возвращаемых символов содержимого поля.
 * 						если данный параметр указать со знаком минус
 * 						содержимое поля будет очищено от HTML-тегов.
 * @return string
 */
function document_get_field_value($field_id, $length = 0, $docid)
{
	if (!is_numeric($field_id)) return '';

	$document_fields = get_document_fields(((isset($docid)) ? $docid : get_current_document_id()));

	$pole = "field_value_".$_SESSION['user_language'];
	$field_value = trim($document_fields[$field_id][$pole]);

	if ($field_value != '')
	{
	$field_value = strip_tags($field_value, "<br /><strong><em><p><i>");

		if (is_numeric($length) && $length != 0)
		{
			if ($length < 0)
			{
				$field_value = strip_tags($field_value);
				$field_value = preg_replace('/  +/', ' ', $field_value);
				$field_value = trim($field_value);
				$length = abs($length);
			}
			$field_value = truncate_text($field_value, $length, '…', true);
		}
	}

	return $field_value;
}

?>